import axios from "axios"
import config from '../../config'
import encodeDecodePassword from "./encodeDecodePassword"
import store from '../module/store'

export default {
    requestMethod(apiName = '', data = {},showLoading = true) {
        return new Promise(async (resolve, reject) => {
            if(showLoading){
                store.setItem('controlLoading', {
                    op: true
                })
            }
            
            let re = await axios.post(config.serverURL + apiName, {
                data: encodeDecodePassword.encodeString(JSON.stringify(data))
            })
            store.setItem('controlLoading', {
                op: false
            })
            re = re.data
            if (re == '非法请求') {
                return resolve(await this.requestMethod(apiName, data))
            } else {
                return resolve(re)
            }
        })
    },
    controlWarningForm(op = false, text = '', callBack = undefined, cancleCallBack = undefined, showCancle = true, okText = '确定', cancleText = '取消') {
        store.setItem('controlWarningForm', {
            op: op,
            text: this.stringDeleteSubString(text, 'Error:'),
            callBack: callBack,
            cancleCallBack: cancleCallBack,
            showCancle: showCancle,
            okText: okText,
            cancleText: cancleText
        })
    },
    controlCoverForm(op = false) {
        store.setItem('controlCoverForm', {
            op: op == true
        })
    },
    wait(n){
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                return resolve()
            }, n*1000);
        })
    },
    stringDeleteSubString(string, subString) {
        string = string.split(subString)
        let re = ''
        for (let i = 0; i < string.length; i++) {
            re += string[i]
        }
        return re
    },
}